<?php
require_once("./define/define_table.php");
require_once("./define/define_text.php");

define("ADD_EQUIPE_FORM", "Formulaire d'ajout de membre dans une equipe");

class Equipe {

	private $id_individu;
	private $id_evenement;

	public function Equipe(){
	}

	public function getIdIndividu(){
		return $this->id_individu;
	}

	public function getIdEvenement(){
		return $this->id_evenement;
	}

	public function setIdIndividu($id){
		$this->id_individu = $id;
	}

	public function setIdEvenement($id){
		$this->id_evenement = $id;
	}

	public function getAllEquipe(){
		global $db;
		try{
			$sql = "SELECT id_individu, id_evenement FROM ".EQUIPE_TABLE;
			$sth = $db->prepare($sql);
			$sth->execute();
			return $sth->fetchAll(PDO::FETCH_CLASS, "Equipe");
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}

	public function getEquipe($id_evenement, $id_individu = null){
		global $db;
		try{
			if($id_individu == null) {
				$sql = "SELECT id_individu, id_evenement  FROM ".EQUIPE_TABLE."
					WHERE id_evenement = :id_evenement";
				$sth = $db->prepare($sql);
				$sth->bindValue(":id_evenement", $id_evenement, PDO::PARAM_INT);
				$sth->execute();
				PrintQuery::queryPrint($sth);
				return $sth->fetchAll(PDO::FETCH_CLASS, "Equipe");
			}
			else {
				$sql = "SELECT id_individu, id_evenement FROM ".EQUIPE_TABLE."
					WHERE id_evenement = :id_evenement AND id_individu = :id_individu";
				$sth = $db->prepare($sql);
				$sth->bindValue(":id_evenement", $id_evenement, PDO::PARAM_INT);
				$sth->bindValue(":id_individu", $id_individu, PDO::PARAM_INT);
				$sth->execute();
				return $sth->fetchObject("Equipe");
			}
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}

	public function addOrModify(){
		global $db;
		$sql = "SELECT COUNT(*) FROM ".EQUIPE_TABLE." WHERE id_individu = :id_individu AND id_evenement = :id_evenement";
		$sth = $db->prepare($sql);
		$sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
		$sth->bindValue(":id_evenement", $this->id_evenement, PDO::PARAM_INT);
		$sth->execute();
		$nb = $sth->fetch();
		if($nb[0] != 0) {
			die(_ALREADY_IN_TEAM_);
		}
		else {
			$sql = "INSERT INTO ".EQUIPE_TABLE." VALUES(:id_individu, :id_evenement)";
			$sth = $db->prepare($sql);
			$sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
			$sth->bindValue(":id_evenement", $this->id_evenement, PDO::PARAM_INT);
			$sth->execute();
			PrintQuery::queryPrint($sth);
		}
	}

	public function getAddForm($id = null){
	    $id_asso = ($id != null) ? Evenement::getEvenement($id)->getIdAssociation() : null;
		$html = ADD_EQUIPE_FORM .
			'<form action="?module=equipe&action=doAdd" method="post">' .
			'	<table>' .
			'		<tr>' .
			'			<td>'._ID_EVENEMENT_.'</td>' .
			'			<td>'.Evenement::generateSelect($id).'</td>' .
			'		</tr>' .
			'		<tr>' .
			'			<td>'._ID_INDIVIDU_.'</td>' .
			'			<td>'.Adherent::generateSelectForAssociation($id_asso).'</td>' .
			'		</tr>' .
			'		<tr>' .
			'			<td colspan="2"><input type="submit" />' .
			'		</tr>' .
			'	</table>' .
			'</form>';
		return $html;
	}

	public function delete(){
		global $db;
		$sql = "DELETE FROM ".EQUIPE_TABLE." WHERE id_individu = :id_individu AND id_evenement = :id_evenement";
		$sth = $db->prepare($sql);
		$sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
		$sth->bindValue(":id_evenement", $this->id_evenement, PDO::PARAM_INT);
		$sth->execute();
		PrintQuery::queryPrint($sth);
	}

	public function deleteAll(){
		global $db;
		$sql = "DELETE FROM ".EQUIPE_TABLE." WHERE id_evenement = :id_evenement";
		$sth = $db->prepare($sql);
		$sth->bindValue(":id_evenement", $this->id_evenement, PDO::PARAM_INT);
		$sth->execute();
		PrintQuery::queryPrint($sth);
	}
}

?>
